package blog.gui;

import javax.microedition.lcdui.*;

import blog.*;
import blog.controller.*;
import blog.db.*;
import java.util.*;

public class BlogSelectGui extends List implements CommandListener, IBaseGui {
	
	public static final int TYPE_ADD_VERIFY = 0;
	public static final int TYPE_USER_CLICK = 1;
	public static final int TYPE_EDIT_VERIFY = 2;
	
	private static Command cmdBack = new Command( ConstString.CMD_BACK, Command.BACK, 0 );
	private static Command cmdSelect = new Command( ConstString.CMD_SELECT, Command.SCREEN, 0 );
	
	private String [] blogIdArray;
	private String [] blogUrlArray;
	
	private int m_type;
	private BlogAccount m_account;
	
	public BlogSelectGui()
	{
		super( ConstString.GUI_TITLE_BLOG_SELECT, List.EXCLUSIVE );
		this.setCommandListener(this);
		this.addCommand(cmdBack);
	}
	
	private static BlogSelectGui _instance;
	public synchronized static BlogSelectGui getInstance()
	{
		if ( _instance==null )
		{
			_instance = new BlogSelectGui();
		}
		return _instance;
	}
	
	public void setAccount( BlogAccount account )
	{
		m_account = account;
	}
	
	public void reloadData( Vector blogsList, int type )
	{
		m_type = type;
		this.deleteAll();
		this.removeCommand(cmdSelect);
		
		int numBlogs = blogsList.size( );
		if ( numBlogs==0 )
		{
			return;
		}
		this.addCommand(cmdSelect);
		if ( blogIdArray==null || blogIdArray.length!=numBlogs )
		{
			blogIdArray = blogUrlArray = null;
			blogIdArray = new String[numBlogs];
			blogUrlArray = new String[numBlogs];
		}
		
//		String sCurBlogID = SysRuntime.curAccount.getBlogId();
		for (int i = 0; i < numBlogs; i++)
		{
			Hashtable blogData = (Hashtable) blogsList.elementAt( i );
			this.append((String) blogData.get( "blogName" ), null);
			blogIdArray[i] = (String) blogData.get( "blogid" );
			blogUrlArray[i] = (String) blogData.get("url");
//			if ( sCurBlogID != null)
//			{
//				if ( sCurBlogID.equals(blogIdArray[i]) )
//				{
//					setSelectedIndex(i, true);
//				}
//			}
		}
	}

	public void commandAction(Command cmd, Displayable arg1) {
		if ( cmd==cmdBack )
		{
			SysRuntime.guiManager.back();
		}
		else if ( cmd==cmdSelect )
		{
			if ( this.m_type==TYPE_USER_CLICK )
			{
				SysRuntime.curAccount.setBlogId( blogIdArray[getSelectedIndex()] );
				SysRuntime.curAccount.setBlogUrl( blogUrlArray[getSelectedIndex()] );
				BlogAccountDB.update( SysRuntime.curAccount );
				SysRuntime.guiManager.back();
			}
			else if ( m_type==TYPE_ADD_VERIFY || m_type==TYPE_EDIT_VERIFY )
			{
				m_account.setBlogId(blogIdArray[getSelectedIndex()]);
				m_account.setBlogUrl( blogUrlArray[getSelectedIndex()] );
				m_account.setChecked( true );
				BlogAccountDB.update(m_account);
				SysRuntime.guiManager.pop();
				BlogAccountManagerGui nextScreen = (BlogAccountManagerGui)SysRuntime.guiManager.pop();
				nextScreen.reloadData();
				SysRuntime.guiManager.show(nextScreen, false);
			}
		}
	}

	public int getIdentify() {
		return ConstGui.GUI_ID_BLOG_SELECT;
	}

	public void notifyChanged(Object param) {

	}

}